<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\V1UserMoneyLog;
use support\Db;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1UserRecharge;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 充值记录
 */
class V1UserRechargeController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需鉴权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1UserRecharge
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1UserRecharge;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        $admin = admin();
        return view('v1-user-recharge/index',compact('admin'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-user-recharge/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-user-recharge/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doDelete($ids);
        return $this->json(0);
    }


    /**
     * 清空未支付记录
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function clear(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $admin = admin();
            if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
                $where['user_id'] = $admin['id'];
            }
            $where['status'] = CommonConstant::DEFAULT_0;
            $ids = $this->model->where($where)->pluck('id')->toArray();
            if (!$ids) {
                return $this->json(1, '没有未支付记录');
            }

            Util::db()->transaction(function () use ($ids) {
                $this->model->whereIn('id', $ids)->delete();
            });
            return $this->json(0, '清空成功');
        }
    }

    /**
     * 在线充值
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function recharge(Request $request): Response
    {
        return view('v1-user-recharge/recharge');
    }

}
